package com.hsd.provider;

import com.hsd.entity.Book;

import java.util.List;
import java.util.Map;

public class BookMapperProvider {

    /**
     * 提供组合查询的动态SQL
     *
     * @param book
     * @return
     */
    public String getSelectDynamicSQL(Book book) {
        String perfixSQL = "select * from t_book where 1=1";
        String joinSQL = "";
        if (book.getName() != null) {
            joinSQL += " and b_name like concat('%',#{name},'%')";
        }
        if (book.getAuthor() != null) {
            joinSQL += " and b_author = #{author}";
        }
        System.out.println("DynamicSQL:" + perfixSQL + joinSQL);
        return perfixSQL + joinSQL;
    }

    /**
     * 提供添加的动态SQL
     *
     * @param map
     * @return
     */
    public String getInsertDynamicSQL(Map<String, Object> map) {
        List<Book> bookList = (List<Book>) map.get("list");
        String perfixSQL = "insert into wu.t_book (b_name, b_author, b_price, b_number, b_time, b_descr) values";
        String joinSQL = "";
        for (Book book : bookList) {
            joinSQL += "('" + book.getName() + "', '" + book.getAuthor() + "', '" + book.getPrice() + "', '" + book.getNumber() + "', '" + book.getTime() + "', '" + book.getDescr() + "')";
        }
        joinSQL = joinSQL.replaceAll("\\)\\(", "),(");// 将所有 )( 结构替换为 ),(
        System.out.println("DynamicSQL:" + perfixSQL + joinSQL);
        return perfixSQL + joinSQL;
    }

    /**
     * 提供删除的动态SQL
     *
     * @param map
     * @return
     */
    public String getDeleteDynamicSQL(Map<String, Object> map) {
        int[] arr = (int[]) map.get("array");
        String perfixSQL = "delete from t_book where b_id in ";
        String joinSQL = "(";
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                //最后一个数据末尾不要“,”
                joinSQL += arr[i];
                break;
            }
            joinSQL += arr[i] + ",";
        }
        joinSQL += ")";
        System.out.println("DynamicSQL:" + perfixSQL + joinSQL);
        return perfixSQL + joinSQL;
    }

    /**
     * 提供更新的动态SQL
     *
     * @param book
     * @return
     */
    public String getUpdateDynamicSQL(Book book) {
        String perfixSQL = "update t_book set ";
        String joinSQL = "";
        if (book.getName() != null) {
            joinSQL += "b_name = #{name},";
        }
        if (book.getAuthor() != null) {
            joinSQL += "b_author = #{author},";
        }
        if (book.getPrice() != null) {
            joinSQL += "b_price = #{price},";
        }
        if (book.getNumber() != null) {
            joinSQL += "b_number = #{number},";
        }
        if (book.getTime() != null) {
            joinSQL += "b_time = #{time},";
        }
        if (book.getDescr() != null) {
            joinSQL += "b_descr = #{descr},";
        }
        if (book.getCategory() != null) {
            joinSQL += "b_category = #{category.id},";
        }
        joinSQL = joinSQL.substring(0, joinSQL.length() - 1);//去掉最后一个“,”
        joinSQL += " where b_id = #{id}"; //拼接条件
        System.out.println("DynamicSQL:" + perfixSQL + joinSQL);
        return perfixSQL + joinSQL;
    }
}
